#include "test.pb.h"

#include <iostream>
#include <string>

using namespace fixbug;

int main()
{
    GetFriendListResponse rsp;
    /**
     * 当成员为类对象时
     */
    ResultCode *result=rsp.mutable_result();    // 获取result字段的地址
    result->set_errcode(0);     // 0表没出错，无错误信息


    /**
     * 当成员为列表时
     */
    User* user1=rsp.add_friend_list();  // 获取最后一个待插入位置的地址
    user1->set_name("sha zeng");
    user1->set_age(18);
    user1->set_sex(User::MAN);

    User* user2=rsp.add_friend_list();
    user2->set_name("bai gu jing");
    user2->set_age(18);
    user2->set_sex(User::WOMAN);
    
    std::string send_str;
    if(rsp.SerializePartialToString(&send_str))
    {
        std::cout<<send_str<<std::endl;
    }

    std::cout << "--------------------------"<< std::endl;

    GetFriendListResponse rsp2;
    if(rsp2.ParseFromString(send_str))  // 接收方进行反序列化
    {
        std::cout<<rsp2.result().errcode()<<std::endl;
        std::cout<<rsp2.result().errmsg()<<std::endl;
        for(int i=0;i<rsp2.friend_list_size();i++)
        {
            std::cout<<rsp2.friend_list(i).name()<<" "
                    <<rsp2.friend_list(i).age()<<" "
                    <<rsp2.friend_list(i).sex()<<std::endl;
        }
    }

    return 0;
}

int main1()
{
    // 把要发送的数据填入
    LoginRequest req;
    req.set_name("sun wu kong");
    req.set_pwd("123456");

    std::string send_str;
    // 序列化成字符串，通过网络发送
    if (req.SerializeToString(&send_str))
    {
        std::cout << send_str << std::endl;
        // \n\014sun wu kong\022\006123456
    }

    // 接收方，从send_str反序列化出一个LoginRequest对象
    LoginRequest req2;
    if (req2.ParseFromString(send_str))
    {
        std::cout << req.name() << std::endl;
        std::cout << req.pwd() << std::endl;
    }

    LoginResponse rsp;
    ResultCode *result = rsp.mutable_result();
    result->set_errcode(1);
    result->set_errmsg("登录失败");
    rsp.set_success(false);

    std::string recv_str;
    if (rsp.SerializePartialToString(&recv_str))
    {
        std::cout << recv_str << std::endl;
    }
    std::cout << "--------------------------"<< std::endl;

    LoginResponse rsp2;
    if (rsp2.ParseFromString(recv_str))
    {
        std::cout << rsp2.result().errcode() << std::endl;
        std::cout << rsp2.result().errmsg() << std::endl;
        std::cout << rsp2.success() << std::endl;
    }


    return 0;
}
